package com.property.demo.repository;

import com.property.demo.model.Home;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface HomeRepository extends JpaRepository<Home, Long>, JpaSpecificationExecutor<Home> {
    
    List<Home> findByBuildingId(Long buildingId);
    
    List<Home> findByOwnerId(Long ownerId);
    
    List<Home> findByNameContainingAndBuildingId(String name, Long buildingId);
    
    List<Home> findByNameContaining(String name);
    
    List<Home> findByStatus(String status);
    
    Boolean existsByNameAndBuildingId(String name, Long buildingId);

    Page<Home> findByBuildingId(Long buildingId, Pageable pageable);
    
    @Query("SELECT COUNT(h) FROM Home h WHERE h.building.community.id = :communityId")
    long countByCommunityId(@Param("communityId") Long communityId);
    
    @Query("SELECT COUNT(h) FROM Home h WHERE h.building.community.id = :communityId AND h.status = :status")
    long countByCommunityIdAndStatus(@Param("communityId") Long communityId, @Param("status") String status);
} 